package com.gmldd.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 工具类
 * @author AdminTH
 *
 */
public class JdbcUtils {

	// 1. 初始化数据源对象
	private static DataSource dataSource;
	static {
		dataSource = new ComboPooledDataSource();
	}

	// 2. 从数据源中获取连接对象
	public static Connection getConnection(){
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	// 3. 关闭连接
	public static void close(Connection con) {
		try {
			if (con != null && !con.isClosed()) {
				con.close();
				con = null; // 建议回收资源
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}


















